﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Infos.InterviewApp.ViewModels;
using LibQuestionIs.Models;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

namespace Infos.InterviewApp.View
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class ScegliDifficolta : Infos.InterviewApp.Common.LayoutAwarePage
    {
        //public ObservableCollection<Difficolta> listaDifficolta { get; set; }
        public ScegliDifficoltaViewModel ViewModel { get; set; }

        public ScegliDifficolta()
        {
            this.InitializeComponent();
            PopupBacheca = new Popup()
            {
                Margin = new Thickness(100, 100, 100, 100),
                VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch,
                HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch,
            };
            ViewModel = new ScegliDifficoltaViewModel();// { Tipologia = DataFlowObject.TipologiaTest.NomeTest };
            DataContext = ViewModel;
            //  boxConferma.DataContext = ViewModel;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            SBHeader.AutoReverse = true;
            SBHeader.RepeatBehavior = Windows.UI.Xaml.Media.Animation.RepeatBehavior.Forever;
            SBHeader.Begin();

            Argomento.Tag = DataFlowObject.TipologiaTest.IDTest;
            Argomento.Text = DataFlowObject.TipologiaTest.NomeTest;

            //      listaDifficolta = DataFlowObject.ListDifficolta;
            //      this.DefaultViewModel["ListaDifficolta"] = listaDifficolta;
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void Difficolta_Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)e.OriginalSource;
            DataFlowObject.Difficolta = DataFlowObject.ListDifficolta.Where(x => x.IDDifficolta == (int)button.Tag).Single();
            ViewModel.DifficoltaScelta = DataFlowObject.Difficolta.Titolo;
            //           boxConferma.DataContext = ViewModel;

            //            boxConferma.DataContext = DataFlowObject;


            DataFlowObject.Difficolta.IsCalculate = true;
            StBoxConfermaApp.Stop();
            StBoxConfermaApp.Begin();
            //            if (this.Frame != null)
            //                this.Frame.Navigate(typeof(Domanda));
        }

        private void BtnBacheca_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            if (PopupBacheca.IsOpen)
                return;
            PopupBacheca.Child = new Bacheca(this);
            PopupBacheca.IsOpen = true;
        }

        private void BtnListaRisultati_Click(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
                this.Frame.Navigate(typeof(ListaRisultati));
        }

        private void mail_Click(object sender, RoutedEventArgs e)
        {
            if (PopupBacheca.IsOpen)
                return;
            PopupBacheca.Child = new InvioSuggerimento(this);
            PopupBacheca.IsOpen = true;
        }

        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            StBoxConfermaDisapp.Stop();
            StBoxConfermaDisapp.Begin();
            StBoxConfermaDisapp.Completed += StBoxConfermaDisapp_Completed;
        }

        void StBoxConfermaDisapp_Completed(object sender, object e)
        {
            StBoxConfermaDisapp = null;
            if (this.Frame != null)
                this.Frame.Navigate(typeof(Domanda));
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            StBoxConfermaDisapp.Stop();
            StBoxConfermaDisapp.Begin();
        }
    }
}
